﻿var Qi = {
	storage: {
		//id: {type: value}
	}
	,saveStorage:function(id,type,value){
		if (!this.storage[id]) this.storage[id] = {};
		this.storage[id][type] = value;
	}
	,loadStorage: function(id){
		var item = this.storage[id];
		if (!item) item = {};
		return item;
	}	
	,removeMember: function(){
		var args = Array.prototype.slice.call(arguments);
		var obj = args.shift();
		for (var i=0,len = args.length; i<len; i++){
			if (obj[args[i]])delete obj[args[i]];
		}
	}
	,styleNames: {
		'background-color': 'backgroundColor'
	}
	,browser:{ //mootools
		engine: ''
		,features: {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)}
		,engines: {
			presto: function(){ // Opera
				return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925));
			}
			,trident: function(){ // IE
				return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4);
			}
			,webkit: function(){ // Safari, Chrome
				return (navigator.taintEnabled) ? false : ((Qi.browser.features.xpath) ? ((Qi.browser.features.query) ? 525 : 420) : 419);
			}
			,gecko: function(){ // Mozilla Firefox
				return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19 : 18);
			}
		}
		,detect: function(){
			for (var engine in this.engines){
				var version = this.engines[engine]();
				if (version){
					this.engine = {name: engine, version: version};
					this.engine[engine] = this.engine[engine + version] = true;
					break;
				}
			}
			return {name: engine, version: version};
		}
	}
	,findEventHandler: function(e){
		var targ;
		if (e===undefined) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
	
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
		return targ;
	}
}

Qi.Hash = function(){};
Qi.Hash.prototype = {
	
};
(function(){
	var __hash = window.Hash;
	if (__hash == undefined){
		var __hash = function(){};
		__hash.constructor = new Qi.Hash();
		__hash.prototype.constructor = __hash;
	}else{
		for (var k in Qi.Hash.prototype){
			if(!__hash.prototype[k])__hash.prototype[k] = Qi.Hash.prototype[k];
		}
	}
})();

Qi.browser.detect();

function echo(){
	var args = Array.prototype.slice.call(arguments);
	var arr = [];

	args.each(function(item){
		var text = item==undefined?'undefined':typeof(item)=='object'?JSON.stringify(item):item.toString();
		arr.push(text);
	});
	alert(arr.join('\n'));
}

String.prototype.toInt = function() {
	return parseInt(this, 10);
}

String.prototype.toFloat = function() {
	return parseFloat(this);
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/, ''); 
}

String.prototype.toMultiply = function(num){
	var output = [];
	for (var i=0;i<num;i++){
		output.push(this);
	}
	return output.join('');
}

String.prototype.camelCase = function(){
	return this.replace(/-\D/g, function(match){
		return match.charAt(1).toUpperCase();
	});
};

Array.prototype.indexOf = function(value) {
	var index = -1;
	for (var i = 0, len = this.length; i < len; i++) {
		if (this[i] == value) return i;
	}
	return index;
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.removeAt = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Array.prototype.clone = function() {
	return this.slice(0);
}
Array.prototype.remove = function(item) {
	var a = [];
	for (var i=0; i<this.length; i++) {
		if (this[i] != item) a.push(this[i]);
	}
	return a;
}

Array.prototype.each = function(fn, bind){
	for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
}
Array.prototype.randomize = function() {
 var i = this.length;
 if (i == 0) return;
 var arr = this.clone();
 while (--i) {
  var rand = Math.floor(Math.random()*(i+1));
  var tmp = arr[i];
  this[i] = arr[rand];
  this[rand] = tmp;
 }
 return arr;
}

//prototype.js
Function.prototype.bind = function (){
	var __method = this;
	var args = Array.prototype.slice.call(arguments);
	var obj = args.shift();
	return function() {
		return __method.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
	}
}

var type = {
	is:{
		Null: function(a){return a===null;},
		Undefined: function(a){return a===undefined;},
		nt: function(a){return(a===null||a===undefined);},
		Function: function(a){return(typeof(a)==='function')?a.constructor.toString().match(/Function/)!==null:false;},
		String: function(a){return(typeof(a)==='string')?true:(typeof(a)==='object')?a.constructor.toString().match(/string/i)!==null:false;},
		Array: function(a){return(typeof(a)==='object')?a.constructor.toString().match(/array/i)!==null||a.length!==undefined:false;},
		Boolean: function(a){return(typeof(a)==='boolean')?true:(typeof(a)==='object')?a.constructor.toString().match(/boolean/i)!==null:false;},
		Date: function(a){return(typeof(a)==='date')?true:(typeof(a)==='object')?a.constructor.toString().match(/date/i)!==null:false;},
		HTML: function(a){return(typeof(a)==='object')?a.constructor.toString().match(/html/i)!==null:false;},
		Number: function(a){return(typeof(a)==='number')?true:(typeof(a)==='object')?a.constructor.toString().match(/Number/)!==null:false;},
		Object: function(a){return(typeof(a)==='object')?a.constructor.toString().match(/object/i)!==null:false;},
		RegExp: function(a){return(typeof(a)==='function')?a.constructor.toString().match(/regexp/i)!==null:false;}
	}
	,of:function(a){
		for(var i in this.is){
			if(this.is[i](a)){
				return i.toLowerCase();
			}
		}
	}
};

/* 簡易版typeof
var type = { 
	of: function(obj) { 
		var type = typeof(obj); 
		if(type == 'object') { 
			if(obj == null) { return 'null'; } 
			else if(obj.constructor.toString().match(/regexp/i) != null) { return 'regexp'; } 
			else if(obj.constructor.toString().match(/date/i) != null) { return 'date'; } 
			else if(obj.constructor.toString().match(/html/i) != null) { return 'html'; } 
			else if(obj.constructor.toString().match(/array/i) != null && typeof(obj.length) != 'undefined') { return 'array'; } 
			else { return 'object'; } 
		} 
		else { return type; } 
	} 
};

*/
